package processingdata.militarybase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import processing.core.PVector;
import Control.MilitaryBase;

public class MilitarySerializer {
	
	static ArrayList<MilitaryBase> militaryBases=new ArrayList<MilitaryBase>();
	public static void main(String args[])throws IOException
	{
		String inputFile="data/finalmilitary.data";
		BufferedReader inputReader=new BufferedReader(new FileReader(new File(inputFile)));
		while(inputReader.ready())
		{
			String inputLine=inputReader.readLine();
			String inputLineParts[]=inputLine.split("\t");
			if(inputLineParts.length==3)
			{
				String lat=inputLineParts[0].replaceAll("\\[", "").replaceAll("\\]", "");
				String lng=inputLineParts[1].replaceAll("\\[", "").replaceAll("\\]", "");
				String military=inputLineParts[2];
				String militaryBaseName="";
				String state="";
				if(military.contains(","))
				{
					militaryBaseName=military.split(",")[0];
					state=military.split(",")[1];	
				}
				System.out.println(lat+"\t"+lng);
				if(lat.trim().equals("")&& lng.trim().equals(""))
				{
					continue;
				}
				float latFloat;
				float lngFloat;
				if(lat.contains(","))
				{
					System.out.println(lat);
					String latParts[]=lat.split(",");
					latFloat=Float.parseFloat(latParts[0].trim());
								
				}
				else
				{
					if(lat.trim().equals(""))
					{
						latFloat=0;
					}
					else
					{
						latFloat=Float.parseFloat(lat);
					}
				}
				
				if(lng.contains(","))
				{
					String lngParts[]=lng.split(",");
					System.out.println(lng);
					lngFloat=Float.parseFloat(lngParts[0].trim());
								
				}
				else
				{
					if(lng.trim().equals(""))
					{
						lngFloat=0;
					}
					else
					{
						lngFloat=Float.parseFloat(lng);
					}
				}	
				PVector coordinates=new PVector(latFloat, lngFloat);
				MilitaryBase base=new MilitaryBase(coordinates,"",state,militaryBaseName);
				if(!militaryBaseName.equals(""))
				{
					militaryBases.add(base);
				//	System.out.println(coordinates);
				}
					
			}
			
			
		}
		serializeList();
		
	}
	static void serializeList() throws FileNotFoundException, IOException
	{
		System.out.println(militaryBases.size());
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("data/military.ser")));
		oos.writeObject(militaryBases);
		oos.close();
	}

}
